<?php
namespace app\admin\controller;

use think\Controller;
use utils\Menu;
use think\Request;
use utils\Auth;

class Base extends Controller {

    protected $manager;

    public function __construct() {
        parent::__construct();

        $noRight = ['admin/login', 'admin/logout', 'admin/check'];

        $controller = strtolower(request()->controller());
        $action = request()->action();
        $currentRequest = $controller . '/' . $action;

        if (!in_array($currentRequest, $noRight)) {
            if (!session('?manager')) {
                return $this->redirect('admin/login');
            }

            $this->manager = session('manager');

            if ($this->manager && isset($this->manager['is_admin']) && $this->manager['is_admin'] != 1) {
                $auth = Auth::instance();
                if (!$auth->check($currentRequest, $this->manager['id'])) {
                    return $this->error('你没有权限访问');
                }
            }
        }

        $menu = new Menu();
        $this->assign('mainMenu', $menu->getMenu());

        $request = Request::instance();
        $nav_act = $request->action() == null ? 'index' : $request->action();
        $nav_act = preg_replace('/(_edit)$/', '_list', $nav_act);
        $this->assign('nav_link', strtolower($request->controller()) . '/' . $nav_act);

        $menu_index = $menu->current_menu();
        $this->assign('menu_index', $menu_index);
        $this->assign('subMenu', $menu->getSubMenu($menu_index['menu']));
        $this->assign('menu', $menu);
        $this->assign('node_index', $menu->currentNode());

        $currentNode = $menu->currentNode();
        if (isset($currentNode['name'])) {
            $this->assign('admin_title', $currentNode['name']);
        }
        
        $this->assign('loginName', $this->manager['name']);
    }
}